﻿/*******************************************************
*类名称：WechatImageTextManager
*版本号：V1.0.0.0
*作者：成俊杰
*CLR版本：4.0.30319.36264
*创建时间：2017-09-06 17:51:48
*说明：T_WechatImageText管理类
*更新备注：
**********************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wechat.Dao;
using Wechat.Domain;
using Wechat.Dto.Request;
using Wechat.Dto.Search;
using Wechat.Util.ReturnMessage;
using Wechat.Util.StateCode;
using Wechat.Dto.Extension;
using Wechat.Util.ParmsConfig;
using Wechat.Dto.Response;

namespace Wechat.Manager.Implement
{
    /// <summary>
    /// T_WechatImageText
    /// </summary>
    public class WechatImageTextManager : GenericManagerBase<WechatImageText>, IWechatImageTextManager
    {
        public IWechatImageTextRepository WechatImageTextRepository { get; set; }
        /// <summary>
        /// 获取微信图文消息
        /// </summary>
        /// <returns></returns>
        public ResultMessage GetWechatImageTextList(WechatImageTextSearch Search)
        {
            ResultMessage returnMsg = new ResultMessage()
            {
                Status = false,
                Message = "查询图文消息失败"
            };
            var imageTextList = WechatImageTextRepository.GetWechatImageTextList(Search);

            if (imageTextList.Count > 0)
            {
                returnMsg.Status = true;
                returnMsg.Data = imageTextList;
                returnMsg.Code = (int)EnumApiStateCode.Success;
                returnMsg.Message = "查询图文消息成功";
            }

            return returnMsg;
        }

        /// <summary>
        /// 获取微信图文信息列表
        /// </summary>
        /// <returns></returns>
        public ResultMessage GetWechatImageTextListTwo(WechatImageTextSearch search)
        {
            ResultMessage returnMsg = new ResultMessage()
            {
                Status = false,
                Message = "查询图文列表失败"
            };

            #region 数据处理

            var imageTextList = WechatImageTextRepository.GetWechatImageTextList(search)
                                .OrderBy(image => image.GroupId)
                                .ThenBy(image => image.Id);
            List<WechatImageTextListResponse> responseList = new List<WechatImageTextListResponse>();

            WechatImageTextListResponse response = null;

            foreach (var item in imageTextList)
            {
                if ((item.GroupId ?? 0) == 0)
                {
                    response = GetImageTextListResponse(item);
                    responseList.Add(response);
                    response = null;
                }
                else
                {
                    if (response == null)
                    {
                        response = GetImageTextListResponse(item);
                    }
                    else if (response.Type == 1 && response.GroupId == item.GroupId)
                    {
                        response.List.Add(new ImagetText()
                        {
                            SubIndex = item.Id,
                            Description = item.Descriptions,
                            PicUrl = item.PicUrl,
                            Title = item.Title
                        });
                    }
                    else
                    {
                        responseList.Add(response);
                        response = GetImageTextListResponse(item);

                    }
                }
            }

            if (response != null)
            {
                responseList.Add(response);
            }
            #endregion

            if (responseList.Count > 0)
            {
                returnMsg.Data = responseList;
                returnMsg.Code = (int)EnumApiStateCode.Success;
                returnMsg.Message = "查询图文列表成功";
                returnMsg.Status = true;
            }
            return returnMsg;
        }

        /// <summary>
        /// 添加图文消息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ResultMessage InsertOrUpdateImageText(WechatImageTextRequest request)
        {
            ResultMessage returnMsg = new ResultMessage()
            {
                Status = false,
                Message = request.Id > 0 ? "修改图文消息失败" : "添加图文消息失败"
            };
            var result = WechatImageTextRepository.InsertOrUpdateImageText(request);

            if (result > 0)
            {
                returnMsg.Data = result;
                returnMsg.Code = (int)EnumApiStateCode.Success;
                returnMsg.Message = request.Id > 0 ? "修改图文消息成功" : "添加图文消息成功"; ;
                returnMsg.Status = true;
            }
            return returnMsg;
        }

        /// <summary>
        /// 删除图文消息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ResultMessage DeleteImageText(string ids)
        {
            ResultMessage returnMsg = new ResultMessage()
            {
                Status = false,
                Message = "参数校验失败"
            };
            if (ids.Length > 0)
            {
                string condition = $" UPDATE T_WechatImageText SET IsDeleted=1 WHERE Id IN ({ids})";
                var result = ExecuteSql(condition, null);
                if (result > 0)
                {
                    returnMsg.Code = (int)EnumApiStateCode.Success;
                    returnMsg.Message = "删除图文消息成功";
                    returnMsg.Status = true;
                    returnMsg.Data = result;
                }
                else
                {
                    returnMsg.Message = "删除图文消息失败";
                }
            }

            return returnMsg;
        }

        private WechatImageTextListResponse GetImageTextListResponse(WechatImageTextResponse item)
        {
            return new WechatImageTextListResponse()
            {
                CreateDate = item.CreateTime.ToShortDateString(),
                Id = item.Id,
                GroupId = item.GroupId ?? 0,
                Banner = new ImagetTextBanner()
                {
                    Title = item.Title,
                    PicUrl = item.PicUrl
                },
                List = new List<ImagetText>(),
                Type = (item.GroupId ?? 0) == 0 ? 0 : 1
            };

        }
    }
}